package task

import (
	"reflect"

	"gddgame.cc/galaxy/common/dsl/game"
	"gddgame.cc/galaxy/utils"
)

const providerName = "task"

func init() {
	_ = game.RegisterGlobalModule(game.ModuleProvider{
		Name:    providerName,
		Depends: []string{"reward"},

		OnInit: func(env *game.Environment) error {
			var err error
			if _, err = env.RegisterModel(GameTaskName, &GameTask{}); err != nil {
				return err
			}
			return nil
		},
	})
}

func GetGameTaskList(env *game.Environment, extendType reflect.Type, insideType reflect.Type, ids []int) ([]*GameTask, error) {
	var tasks []GameTask
	model, err := env.GetModel(GameTaskName)
	if err != nil {
		return nil, err
	}
	if _, err := model.Query().Where("in", "id", ids).Find(&tasks); err != nil {
		return nil, err
	}
	// 绑定于task关系
	taskList := make([]*GameTask, len(tasks))
	for k := range tasks {
		if extendType != nil {
			tasks[k].Extend = utils.ToType(tasks[k].Extend, extendType)
		}
		if insideType != nil {
			tasks[k].Inside = utils.ToType(tasks[k].Inside, insideType)
		}
		taskList[k] = &tasks[k]
	}
	return taskList, nil
}
